package com.sloera.java.common.tool.entity;

import com.sloera.java.common.tool.service.IResult;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;

import java.io.Serializable;

/**
 * @author SloeraN
 * @version 1.0
 * @class com.sloera.java.common.tool.entity.R
 * @date 2020/8/12
 */
@Getter
@Setter
@ToString
@NoArgsConstructor
public class R<T> implements Serializable {
    /**
     * 成功标志
     */
    private boolean success;
    /**
     * 状态码
     */
    private int code;
    /**
     * 消息
     */
    private String msg;
    /**
     * 承载数据
     */
    private T data;

    /**
     * 默认成功消息
     */
    private static final String DEFAULT_SUCCESS_MESSAGE = "操作成功";

    /**
     * 默认失败消息
     */
    private static final String DEFAULT_FAILURE_MESSAGE = "操作失败";

    /**
     * 默认为空消息
     */
    private static final String DEFAULT_NULL_MESSAGE = "暂无数据";

    /**
     * @param code 状态码
     * @param data 数据
     * @param msg 消息
     * @date 2020/8/12
     */
    private R(int code, T data, String msg) {
        this.code = code;
        this.data = data;
        this.msg = msg;
        this.success = CommonResultCode.SUCCESS.code == code;
    }

    private R(IResult result, T data, String msg) {
        this(result.getCode(), data, msg);
    }

    private R(IResult result, String msg) {
        this(result, null, msg);
    }

    private R(IResult result, T data) {
        this(result, data, result.getMessage());
    }

    private R(IResult result) {
        this(result, null, result.getMessage());
    }

    /**
     * @param code 状态码
     * @param data 数据
     * @param msg 消息
     * @param <T> T 泛型标记
     * @return R
     */
    public static <T> R<T> data(int code, T data, String msg) {
        return new R<>(code, data, data == null ? DEFAULT_NULL_MESSAGE : msg);
    }

    /**
     * @param data 数据
     * @param msg 消息
     * @param <T> T 泛型标记
     * @return R
     */
    public static <T> R<T> data(T data, String msg) {
        return data(CommonResultCode.SUCCESS.getCode(), data, msg);
    }

    /**
     * @param data 数据
     * @param <T> T 泛型标记
     * @return R
     */
    public static <T> R<T> data(T data) {
        return data(data, DEFAULT_SUCCESS_MESSAGE);
    }

    public static <T> R<T> success(String msg) {
        return new R<>(CommonResultCode.SUCCESS, msg);
    }

    public static <T> R<T> success(T data) {
        return new R<>(CommonResultCode.SUCCESS, data);
    }

    public static <T> R<T> success(IResult result) {
        return new R<>(result);
    }

    public static <T> R<T> success(IResult result, String msg) {
        return new R<>(result, msg);
    }


    public static <T> R<T> fail(String msg) {
        return new R<>(CommonResultCode.FAILURE, msg);
    }

    public static <T> R<T> fail(T data) {
        return new R<>(CommonResultCode.FAILURE, data);
    }

    public static <T> R<T> fail(IResult result) {
        return new R<>(result);
    }

    public static <T> R<T> fail(IResult result, String msg) {
        return new R<>(result, msg);
    }

}
